home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / autofind / media / mediaimp.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  10.6 KB  |  389 lines  |  [TEXT/ttxt]

  1. --<<<-
  2. -- Filename:
  3. --    mediaimp.sx
  4.  
  5. -- Other Files Required:
  6. --    This file is not part of any specific example, but rather a useful class for
  7. --    importing media and storing the media into ObjectStore.
  8.  
  9. -- Purpose:
  10. --     Provide a layer to the import export engine which enables easy use of ObjectStore. 
  11.  
  12. -- Specialized Classes:
  13. --     MediaImporter
  14.  
  15. -- Instructions to User:
  16. --    Class MediaImporter provides a layer to the import export engine
  17. --    which enables easy use of ObjectStore. When you create a MediaImporter,
  18. --    you may specify a library container in which media is automatically stored.
  19. --  The target collection of the library container may be either an explicitly or
  20. --  implicitly keyed collection subclass. When you are done importing, you can invoke
  21. --  the MediaImporter saveMedia method to close the library container.
  22. --    For each import method, you specify a fileName and optional mediaKey. The
  23. --    import adds an entry into the ExplicitKeyedCollection using the filename as
  24. --    the key, unless the mediaKey is supplied. In this case, mediaKey is used as
  25. --    the key.
  26.  
  27. --    PUBLIC PROTOCOL
  28. --    importObject self fileName key:mediaKey
  29. --    importDIB self fileName key:mediaKey
  30. --    importPICT self fileName key:mediaKey
  31. --    importRTF self fileName key:mediaKey
  32. --    importAIFF self fileName key:mediaKey
  33. --    importWave self fileName key:mediaKey
  34. --    importQuicktime self fileName key:mediaKey
  35. --    importRes self type id key:mediaKey
  36. --    save self name
  37.  
  38. --    KEYWORD ARGUMENTS
  39. --    dir            -- dir from which to import media.
  40. --    container        -- library container for media
  41. --    extension        -- extension to add
  42. --    mediaCategory    -- @image, @sound, @movie
  43. --    inputMediaType    -- @dib, @pict, etc.
  44. --    outputMediaType    -- @bitmap, @player, etc.
  45.  
  46. --    EXAMPLE CREATION
  47. --    An importer which imports from the StartDir:
  48. --    mi := new MediaImporter
  49.  
  50. --    An importer which imports from a folder called "media", and stores media in
  51. --    library container c
  52. --    mi := new MediaImporter dir:(spawn theStartDir "media") container:c
  53.  
  54. -- Author:
  55. --     Steve Mayer
  56.  
  57. in module AutofinderMedia
  58.  
  59. class MediaImporter ()
  60. class variables
  61.     extensionMapping    -- mapping of file extensions to import parameters.
  62. instance variables
  63.     dir                -- directory from which to import media.
  64.     container            -- library container for media
  65.     addExtensions        -- if true, file extensions will be added for specific media.
  66.     smartExtensions    -- if true, file type is inferred from extension.
  67.     extension        -- extension to add
  68.     mediaCategory    -- @image, @sound, @movie
  69.     inputMediaType    -- @dib, @pict, etc.
  70.     outputMediaType    -- @bitmap, @player, etc.
  71.     bundle            -- ResBundle instance, if we are importing Res files.
  72.     convertToShapes    -- If true, convert bitmaps to twodshapes.
  73.     shrinkBitmaps        -- If true, shrink wrap bitmaps.
  74.     matteColor        -- if defined, set bitmap's matte to this color.
  75.     invisibleColor        -- if defined, set bitmaps's invisible color to this color.
  76. class methods
  77.     method init self #rest args ->
  78.     (
  79.         apply nextMethod self args
  80.         local em := new KeyedLinkedList
  81.         add em ".dib" #(@Image, @DIB, @Bitmap)
  82.         add em ".bmp" #(@Image, @DIB, @Bitmap)
  83.         add em ".pic" #(@Image, @PICT, @Bitmap)
  84.         add em ".aif" #(@Sound, @AIFF, @Player)
  85.         add em ".wav" #(@Sound, @Wave, @Player)
  86.         add em ".avi" #(@Movie, @AVI, @Player)
  87.         add em ".mov" #(@Movie, @Quicktime, @Player)
  88.         add em ".rtf" #(@Text, @RTF, @RichText)
  89.         self.extensionMapping := em
  90.         self
  91.     )
  92. end
  93.  
  94. -- Method init saves keyword arguments in instance variables.
  95. method init self {class MediaImporter} #rest args #key \
  96.     dir: (theStartDir) \
  97.     container: (undefined) \
  98.     addExtensions: (false) \
  99.     smartExtensions: (false) \
  100.     mediaCategory: (@image) \
  101.     inputMediaType: (@dib) \
  102.     outputMediaType: (@bitmap) \
  103.     convertToShapes: (true) \
  104.     shrinkBitmaps: (false) \
  105.     mode: (@create) ->
  106. (
  107.     apply nextMethod self args
  108.     self.dir := dir
  109.     self.container := container
  110.     self.addExtensions := addExtensions
  111.     self.smartExtensions := smartExtensions
  112.     self.mediaCategory := mediaCategory
  113.     self.inputMediaType := inputMediaType
  114.     self.outputMediaType := outputMediaType
  115.     self.matteColor := undefined
  116.     self.invisibleColor := undefined
  117.     self.convertToShapes := convertToShapes
  118.     self.shrinkBitmaps := shrinkBitmaps
  119.     return self
  120. )
  121.  
  122. method getOne self {class MediaImporter} key ->
  123. (
  124.     getOne self.container key
  125. )
  126.  
  127. -- Method inferMediaType parses a filename to get its extension, and then
  128. -- calls setExtension on self.
  129. method inferMediaType self {class MediaImporter} fileName ->
  130. (
  131.     -- Get extension part of filename.
  132.     local ext := new String
  133.     for i := ((size fileName) - 3) to (size fileName) do
  134.     (
  135.         append ext fileName[i]
  136.     )
  137.     setExtension self ext
  138. )
  139.  
  140. method stripExtension self {class MediaImporter} fileName ->
  141. (
  142.     -- Get prefix part of filename.
  143.     local prefx := new String
  144.     for i := 1 to ((size fileName) - 4) do
  145.     (
  146.         append prefx fileName[i]
  147.     )
  148.     return (prefx as StringConstant)
  149. )
  150.  
  151. -- Method setExtension sets the current file extension to the specified
  152. -- value. It also automatically sets the import parameters based the
  153. -- MediaImporter.extensionMapping.
  154. method setExtension self {class MediaImporter} extension ->
  155. (
  156.     self.extension := extension
  157.     local result := MediaImporter.extensionMapping[extension]
  158.     if (result <> empty) do
  159.     (
  160.         self.mediaCategory := result[1]
  161.         self.inputMediaType := result[2]
  162.         self.outputMediaType := result[3]
  163.     )
  164. )
  165.  
  166. -- Method shrinkBitmap "shrink-wraps" a bitmap.
  167. method shrinkBitmap self {class MediaImporter} bm ->
  168. (
  169.     local box := bm.bbox
  170.     local w := bm.bbox.width
  171.     local h := bm.bbox.height
  172.     local left := w, right := -1, top := h, bottom := -1
  173.     local rowbytes := bm.rowbytes
  174.     local data := bm.data
  175.     local bg := data[1]
  176.     local x := 0
  177.     local y := 0
  178.     foreach data (p xxx ->
  179.         if ((p != bg) and (x < w)) do (
  180.             if (x < left) do (left := x)
  181.             if (x > right) do (right := x)
  182.             if (y < top) do (top := y)
  183.             if (y > bottom) do (bottom := y)
  184.         )
  185.         x := x + 1
  186.         if (x == rowbytes) do (
  187.             x := 0
  188.             y := y + 1
  189.         )
  190.     ) ok
  191.     
  192.     bottom := bottom + 1
  193.     right := right + 1
  194.     if ((top = h) or (left = w) or (right = 0) or (bottom = 0)) do (
  195.         top := 0
  196.         left := 0
  197.         right := 0
  198.         bottom := 0
  199.     )
  200.     local box := new Rect x1:left y1:top x2:right y2:bottom
  201. print box
  202.     local bms := new BitmapSurface colormap: bm.colormap bbox: box
  203.     transfer bms (bm as BitmapSurface) bms (new TwoDMatrix)
  204.     print #("Shrinking bitmap", #(w, h), "to", #(left, top, right, bottom),
  205.             "saving", bm.data.size, bms.data.size, bm.data.size - bms.data.size)
  206.  
  207.     bms as Bitmap
  208. )
  209.  
  210. -- Method processBitmap is called to process an imported bitmap, based on
  211. -- MediaImporter parameters.
  212. method processBitmap self {class MediaImporter} bm ->
  213. (
  214.     if (self.shrinkBitmaps) do
  215.     (
  216.         bm := shrinkBitmap self bm
  217.     )
  218.     if (self.invisibleColor <> undefined) do
  219.     (
  220.         bm.invisibleColor := self.invisibleColor
  221.     )
  222.     if (self.matteColor <> undefined) do
  223.     (
  224.         bm.matteColor := self.matteColor
  225.     )
  226.     if (self.convertToShapes) do
  227.     (
  228.         bm := new TwoDShape boundary:bm fill:blackBrush
  229.     )
  230.     return bm
  231. )
  232.  
  233. -- Method storeMedia handles adding the media to the library container
  234. method storeMedia self {class MediaImporter} name media ->
  235. (
  236.     -- proxy for media if using a library container.
  237.     if self.container <> undefined do
  238.     (
  239.         -- Add the media to the container.
  240.         if (isAKindOf self.container.targetCollection ImplicitlyKeyedCollection) then
  241.         (
  242.             append self.container media
  243.         ) else
  244.         (
  245.             add self.container name media
  246.         )
  247. --        makePurgeable media
  248.     )
  249. )
  250.  
  251. method importObject self {class MediaImporter} fileName #key mediaKey: (undefined) \
  252.     mediaStream:(undefined) ->
  253. (
  254.     local mStream
  255.     local mKey := fileName
  256.     if (mediaStream <> undefined) then
  257.     (
  258.         mStream := mediaStream
  259.     ) else
  260.     (
  261.         -- Create a media stream for the specified dir, and convert to output media type.
  262.         if (self.addExtensions) do
  263.         (
  264.             fileName := fileName + self.extension
  265.         )
  266.         
  267.         if (self.smartExtensions) do
  268.         (
  269.             -- Infer media type, and strip extension from filename.
  270.             inferMediaType self fileName
  271.             mKey := stripExtension self fileName
  272.         )
  273.         -- Build path for Media folder and append fileName.
  274.         local p := copy (self.dir as Sequence)
  275.         append p fileName
  276.         mStream := getStream theRootDir p @Readable
  277.     )
  278.  
  279. -- This is no longer needed, right!??
  280. --    local storageCont := undefined
  281. --    if (self.container <> undefined) do
  282. --    (
  283. --        storageCont := self.container.storageContainer
  284. --    )
  285.         
  286.     local media := importMedia theImportExportEngine mStream \
  287.         self.mediaCategory self.inputMediaType self.outputMediaType \
  288.         container:self.container colorMap: defaultColorMap
  289.  
  290.     -- If a mediaKey is specified, use it.
  291.     if (mediaKey <>  undefined) do mKey := mediaKey
  292.     
  293.     -- If it is a bitmap, call the processBitmap method.
  294.     if (self.mediaCategory = @Image) do
  295.     (
  296.         media := processBitmap self media
  297.     )
  298.  
  299.     -- Store the media.
  300.     storeMedia self mKey media
  301. -- What does this do?
  302.     plug mStream
  303.     return media
  304. )
  305.  
  306. -- Method importAllMedia iterates through every file in the media importer's dir,
  307. -- and calls importObject.
  308. method importAllMedia self {class MediaImporter} ->
  309. (
  310.     -- Iterate through all files in the dir.
  311.     local failList := new Array
  312.     local theErr := @all
  313.     -- Iterate through all of the files.
  314.     for fileName in (getContents self.dir) do
  315.     (
  316.         -- If it is a file (not a directory)...
  317.         if (isFile self.dir fileName) do
  318.         (
  319.             guard
  320.             (
  321.                 importObject self fileName
  322.             )
  323.             catching
  324.                 theErr : (
  325.                     append failList fileName
  326.                     caught theErr
  327.                     )
  328.             on exit
  329.                 undefined
  330.             end
  331.         )
  332.     )
  333. )
  334.  
  335. -- The following methods support specific media types, and use the more
  336. -- general importObject method.
  337. method importPICT self {class MediaImporter} fileName #key mediaKey: ->
  338. (
  339.     setExtension self ".pic"
  340.     return importObject self fileName mediaKey:mediaKey
  341. )
  342.  
  343. method importRTF self {class MediaImporter} fileName #key mediaKey: ->
  344. (
  345.     setExtension self ".rtf"
  346.     return importObject self fileName mediaKey:mediaKey
  347. )
  348.  
  349. method importAIFF self {class MediaImporter} fileName #key mediaKey: ->
  350. (
  351.     setExtension self ".aif"
  352.     return importObject self fileName mediaKey:mediaKey
  353. )
  354.  
  355. method importWave self {class MediaImporter} fileName #key mediaKey: ->
  356. (
  357.     setExtension self ".wav"
  358.     return importObject self fileName mediaKey:mediaKey
  359. )
  360.  
  361. method importDIB self {class MediaImporter} fileName #key mediaKey: ->
  362. (
  363.     setExtension self ".bmp"
  364.     return importObject self fileName mediaKey:mediaKey
  365. )
  366.  
  367. method importQuicktime self {class MediaImporter} fileName #key mediaKey: ->
  368. (
  369.     setExtension self ".mov"
  370.     return importObject self fileName mediaKey:mediaKey
  371. )
  372.  
  373. method importRes self {class MediaImporter} type id #key mediaKey: (undefined) ->
  374. (
  375.     setExtension self ".pic"
  376.  
  377.     -- If no media key is specified, use the res file id.
  378.     local str := getOneStream self.bundle type:type ID:id
  379.     return importObject self (id as String) mediaStream:str mediaKey:mediaKey
  380. )
  381.     
  382. -- Close the container.
  383. method save self {class MediaImporter} ->
  384. (
  385.     close self.container
  386. )
  387. "Compiled mediaimp.sx"
  388. -->>>
  389.